using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Controls.UserCP
{
    public partial class Register : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Managers.UserLoginManager.CurrentUser != null)
                {
                    mviewRegister.SetActiveView(viewError);
                }
                else
                {
                    mviewRegister.SetActiveView(viewRegister);
                }
            }
        }
        protected void customUsername_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = DAL.Users.GetByUsername(args.Value.Trim()) == null;
        }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                DAL.User user = new DAL.User();
                user.Username = txtUsername.Text.Trim();
                DAL.Users.Add(user, txtPassword.Text);

                lblUsername.Text = Helpers.StringHelper.SafeFormat(user.Username);
                mviewRegister.SetActiveView(viewWelcome);
            }
        }
}
}